﻿using System.Web;
using Newtonsoft.Json;
using NPMonitor.Implementation;
using NPMonitor.DataModel;

namespace NPMonitor.Web.Handler
{
    /// <summary>
    /// WorkflowHandler 的摘要说明
    /// </summary>
    public class WorkflowHandler : IHttpHandler
    {
        protected HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }

        protected HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;
            }
        }

        private HttpContext context;

        public void ProcessRequest(HttpContext context)
        {
            this.context = context;
            context.Response.ContentType = "text/plain";
            string operation = GetParamsValue("OP");
            switch (operation)
            {
                case "ValidateWorkflowName":
                    ValidateWorkflowName();
                    break;
                case "CheckWorkflow":
                    ValidateWorkflowSchedule();
                    break;
            }
        }

        public string GetParamsValue(string key)
        {
            //return context.Request.Params[key];
            if (string.IsNullOrEmpty(Request.QueryString[key]))
                return "";
            else
                return Request.QueryString[key].Trim();
        }

        public void ValidateWorkflowName()
        {
            string workflowId = GetParamsValue("WorkflowId");
            string workflowName = GetParamsValue("WorkflowName");

            int intWorkflowId = 0;
            int.TryParse(workflowId, out intWorkflowId);
            bool status = true;
            string message = string.Empty;

            WorkflowData info = StoreManager.Current.WorkflowStore.GetWorkflowDataByName(workflowName);

            if (info != null)
            {
                if (intWorkflowId != info.WorkflowId.Value)
                {
                    status = false;
                    message = ValidationString.FieldAlreadyExists.FormatString("workflow name");
                }
            }
            var result = new
            {
                Status = status,
                Message = message
            };
            context.Response.Write(JsonConvert.SerializeObject(result));
        }

        public void ValidateWorkflowSchedule()
        {
            string strWorkflowId = GetParamsValue("workflowid");

            int workflowId;

            string message = string.Empty;

            bool status = true;

            if (int.TryParse(strWorkflowId, out workflowId))
            {
                var taskInDataBase = StoreManager.Current.JobStore.GetScheduledTaskById(workflowId);

                if (taskInDataBase.FindAll(x => x.IsDelete == false).Count > 0)
                {
                    status = false;
                    message = CommonString.WorkflowInSchedule;
                }
            }

            var msg = new
            {
                status = status,
                msg = message
            };

            string msgToSend = JsonConvert.SerializeObject(msg);

            Response.Write(msgToSend);

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}